package pl.edu.pb.wi.cookbook.entity;

import java.io.Serializable;

import javax.persistence.Id;

import pl.edu.pb.wi.cookbook.util.FullTextSearchUtil;

import com.googlecode.objectify.Key;

public class Recipe extends FullTextSearch implements Serializable {
    private static final long serialVersionUID = 559841281688671475L;

    @Id
    private Long id;
    private Key<User> author;
    private Key<Category> category;
    private String name;
    private String shortDescription;
    private String description;
    private Integer positiveVoteCount;
    private Integer negativeVoteCount;
    private String ingredients;
    private Integer prepareTime;
    private Integer servings;
    private String imageUrl;
    private Boolean isPrivate;

    public Recipe() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Key<User> getAuthor() {
        return author;
    }

    public void setAuthor(Key<User> author) {
        this.author = author;
    }

    public Key<Category> getCategory() {
        return category;
    }

    public void setCategory(Key<Category> category) {
        this.category = category;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getShortDescription() {
        return shortDescription;
    }

    public void setShortDescription(String shortDescription) {
        this.shortDescription = shortDescription;
    }

    public String getDescription() {
        return description != null ? description.replace("<br>", "<br />")
                .replaceAll("<img[^>]*>", "")
                .replaceAll("<script[\\s\\S]*?>[\\s\\S]*?</script>", "")
                : description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getIngredients() {
        return ingredients != null ? ingredients.replace("<br>", "<br />")
                .replaceAll("<img[^>]*>", "")
                .replaceAll("<script[\\s\\S]*?>[\\s\\S]*?</script>", "")
                : ingredients;
    }

    public void setIngredients(String ingredients) {
        this.ingredients = ingredients;
    }

    public Integer getPrepareTime() {
        return prepareTime;
    }

    public void setPrepareTime(int prepareTime) {
        this.prepareTime = prepareTime;
    }

    public Integer getServings() {
        return servings;
    }

    public void setServings(int servings) {
        this.servings = servings;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public Boolean isPrivate() {
        return isPrivate;
    }

    public Boolean getIsPrivate() {
        return isPrivate;
    }

    public void setPrivate(boolean isPrivate) {
        this.isPrivate = isPrivate;
    }

    public Integer getPositiveVoteCount() {
        return positiveVoteCount;
    }

    public void setPositiveVoteCount(Integer positiveVoteCount) {
        this.positiveVoteCount = positiveVoteCount;
    }

    public Integer getNegativeVoteCount() {
        return negativeVoteCount;
    }

    public void setNegativeVoteCount(Integer negativeVoteCount) {
        this.negativeVoteCount = negativeVoteCount;
    }

    public float getVotePercent() {
        if (positiveVoteCount == 0 && negativeVoteCount == 0)
            return 0;
        else
            return ((float) positiveVoteCount * 100f)
                    / ((float) positiveVoteCount + (float) negativeVoteCount);
    }

    @Override
    public void updateFts() {
        StringBuilder sb = new StringBuilder("");

        if (!isPrivate) {
            sb = sb.append(name).append(" ").append(shortDescription)
                    .append(" ");

            if (description != null)
                sb = sb.append(
                        description.replaceAll("%d", " ").replaceAll("\\s+",
                                " ")).append(" ");

            if (ingredients != null)
                sb = sb.append(
                        ingredients.replaceAll("%d", " ").replaceAll("\\s+",
                                " ")).append(" ");

            if (servings != null)
                sb.append(String.format("dla %d osób", servings)).append(" ");

            if (prepareTime != null)
                sb.append(String.format("%d minut", servings)).append(" ");
        }

        fts = FullTextSearchUtil.getTokensForIndexingOrQuery(sb.toString(),
                FullTextSearchUtil.MAXIMUM_NUMBER_OF_WORDS_TO_PUT_IN_INDEX);
    }
}
